local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseObject())
local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();
local UIManager = ClientOHOH.load.UIManager();
local LanguageHelper= _G.LanguageHelper
local CarTool=_G.CommonOHOH.load.CarTool();
ClientOHOH.load.PartCell()

local fuseAmount = _G.CommonOHOH.config.Game.fuseAmount
local instStr={
   fuseBox="ctrl.fuse.root",

   fuseSelect="ctrl.fuse.btn.select",
   fuseAll="ctrl.fuse.btn.all",

   list="ctrl.fuse.btn.cellBox",

}

function M.new(...)
	return M(...);
end;

function M.ctor(self,ctrl)
   M.super.ctor(self);
   self.ctrl=ctrl
   self.ui=ctrl:InitUiInst2(ctrl.main_inst,instStr)
   self.FuseList = {}--熔断列表
   self.FuseAllList = {}--熔断列表
   self.CellList = {}--展示列表
   --self.isLock = true

   CallBackManager:AddRbxConnect(self,self.ui.fuseSelect.Activated, function()
      print("——————————————————————熔断 选择的————————————————————————————————",self.FuseList)
      if next(self.FuseList) then
      self:FuseSelect()
      else
         print("空")
      end
	end);
   CallBackManager:AddRbxConnect(self,self.ui.fuseAll.Activated, function()
      print("——————————————————————熔断 所有的————————————————————————————————")
      self:FuseAll()
	end);
end;

function M.dtor(self)
	M.super.dtor(self);
end;

--鼠标点击
function M.CellClick(self, cell)
   local carId = cell:GetCarId()

   if cell:isSelected() then
       cell:SetSelectedStatus(false)
       self:FuseListRemove(carId)  -- Custom function to remove from FuseList
   else
       cell:SetSelectedStatus(true)
       table.insert(self.FuseList, carId)  -- Add to FuseList
   end
end
--取消点击
function M:FuseListRemove(carId)
   for i, c in ipairs(self.FuseList) do
       if c == carId then
           table.remove(self.FuseList, i)
           break
       end
   end
end

function M.CreateCell(self,carId,hadnum)
   local cell= require(script.Parent.PartCell).new(carId,self.ui.list,self.ctrl:GetDataBase(),nil,hadnum)
   cell:SetTitleVisibility(false)
   cell:SetPartWeightVisibility(true)
   cell:SetPartInVisibility(true)
   cell:SetPartNum(hadnum)
   cell:SetPartNumVisibility(true)

   cell.ClickEvent:Connect(function()
         self:CellClick(cell)
  end)
    return cell
end


function M.UpdateBox(self,data)
  
   local hadData = data["had"]
   local fliterData = {}

--    for id,cell in pairs(self.CellList) do
--       if not hadData[id] then --不存在
--          print("销毁")
--           cell:dtor() --销毁
--           self.CellList[id]=nil
--       end
--   end
--过滤小于熔断数量 和· 最高品阶的部件
   
   for carId,count in pairs(data.had) do
      local partId,grade = CarTool.GetPartIdAndGrade(carId)
      if count >= fuseAmount  and  grade < 5 then
         fliterData[carId] = count
      end
   end

   for id,cell in pairs(self.CellList) do
      if not fliterData[id] or  not hadData[id] then --不存在
         print("销毁")
          cell:dtor() --销毁
          self.CellList[id]=nil
      end
  end
   -- print("更新熔断表",self.CellList,fliterData)
   for carId,count in pairs(fliterData) do
      if carId and count then
         if self.CellList[carId] then
         --存在cell
             print("存在")
            if  self.CellList[carId]:GetCarCount() == count then
            --不改变
            else
            --改变数值
               self.CellList[carId]:SetPartNum(count)
            end
            
         else
         --不存在cell
         print("不存在")
            self.CellList[carId]=self:CreateCell(carId, count)
         end
      end
   end

   self.FuseAllList = {}
    for carId in pairs(self.CellList) do
        table.insert(self.FuseAllList, carId)
    end
end
function M.FuseSelect(self)
   --return self.ctrl.RelicMakeEvent:Action(rCfg)--recipeID
   self.ctrl.FuseEvent:Action(self.FuseList)--recipeID
   self.FuseList ={}
end
function M.FuseAll(self)
   print(self.FuseAllList)
   self.ctrl.FuseEvent:Action(self.FuseAllList)
   self.FuseAllList = {}
   --return self.ctrl.RelicMakeEvent:Action(rCfg)--recipeID
end
function M.Hide(self)
   self.ui.fuseBox.Visible = false 
end
function M.Show(self)
   self.ui.fuseBox.Visible = true
end



return M